/**
 * 
 */
package com.hera.process.config.rest.delegate.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.hera.base.domain.WrapperJSON;
import com.hera.base.rest.delegate.BaseDelegate;
import com.hera.base.util.FiltroDTO;
import com.hera.process.config.domain.BusinessDay;
import com.hera.process.config.domain.BusinessHour;
import com.hera.process.config.domain.Calendar;
import com.hera.process.config.domain.Condition;
import com.hera.process.config.domain.Datasource;
import com.hera.process.config.domain.Department;
import com.hera.process.config.domain.Holiday;
import com.hera.process.config.domain.LoginMethod;
import com.hera.process.config.domain.Organization;
import com.hera.process.config.domain.Parameter;
import com.hera.process.config.domain.ParameterDetail;
import com.hera.process.config.domain.Secuence;
import com.hera.process.config.domain.SistVariable;
import com.hera.process.config.domain.Theme;
import com.hera.process.config.rest.delegate.ConfigDelegate;
import com.hera.process.external.domain.AddTableDetail;
import com.hera.process.external.domain.AddTableIndex;
import com.hera.process.external.domain.AdditionalTable;
import com.hera.process.form.domain.Dynaform;
import com.hera.process.form.domain.DynaformField;
import com.hera.process.form.domain.DynaformStage;
import com.hera.process.form.domain.FieldOption;
import com.hera.process.form.domain.FormField;
import com.hera.process.security.domain.SecGroupModule;
import com.hera.process.security.domain.SecurityGroup;
import com.hera.process.security.domain.User;
import com.hera.process.workflow.domain.ProcInstancHistory;
import com.hera.process.workflow.domain.ProcInstancNote;
import com.hera.process.workflow.domain.Process;
import com.hera.process.workflow.domain.ProcessCategory;
import com.hera.process.workflow.domain.ProcessGroup;
import com.hera.process.workflow.domain.ProcessGroupUser;
import com.hera.process.workflow.domain.ProcessInstance;
import com.hera.process.workflow.domain.ProcessSupervisor;
import com.hera.process.workflow.domain.Stage;
import com.hera.process.workflow.domain.StageRollup;
import com.hera.process.workflow.domain.StageUser;
import com.hera.process.workflow.domain.Swimlane;
import com.hera.process.workflow.domain.TaskInstanceCache;
import com.hera.process.workflow.domain.TaskUser;

/**
 * @author cmontes
 *
 */
@Service
public class ConfigDelegateImpl extends BaseDelegate implements ConfigDelegate{

	@Autowired
	private RestTemplate restTemplate;
	
	@Value("${url.contexto.config}")
	private String urlController;
	
	@Value("${url.base}")
	private String urlBase;
	
	private static final Logger log = Logger.getLogger(ConfigDelegateImpl.class);
	
	public ConfigDelegateImpl() {
		log.info("Clase Intanceada ConfigDelegateImpl");
	}
	
	
	
	/*
	///////////////////////////////////////////////////////////////////////////////////
	 /////////////////////////////// BUSINESSDAY ///////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////
	*/
	
	@Override
	public List<BusinessDay> findAllBusinessDay(FiltroDTO filtroDTO) {
        
        return postForObject(urlBase+urlController+"busynessday/list", filtroDTO, 
        		new ParameterizedTypeReference<WrapperJSON<List<BusinessDay>>>() {});
	}

	@Override
	public BusinessDay createBusinessDay(BusinessDay bussinessday) {
		
		   return postForObject(urlBase+urlController+"busynessday/save", bussinessday, 
	        		new ParameterizedTypeReference<WrapperJSON<BusinessDay>>() {});
	}

	@Override
	public BusinessDay updateBusinessDay(BusinessDay bussinessday) {
		
		return postForObject(urlBase+urlController+"busynessday/update", bussinessday, 
        		new ParameterizedTypeReference<WrapperJSON<BusinessDay>>() {});
	}

	@Override
	public void deleteBusinessDay(BusinessDay bussinessday) {
		
		postForObject(urlBase+urlController+"busynessday/delete", bussinessday, 
        		new ParameterizedTypeReference<WrapperJSON<BusinessDay>>() {});
	}
	
	/*
	///////////////////////////////////////////////////////////////////////////////////
	 /////////////////////////////// BUSINESSHOUR ///////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////
	*/
	
	@Override
	public List<BusinessHour> findAllBusinessHours(FiltroDTO filtroDTO) {
	     
        return postForObject(urlBase+urlController+"businesshour/list", filtroDTO, 
        		new ParameterizedTypeReference<WrapperJSON<List<BusinessHour>>>() {});
	}

	@Override
	public BusinessHour createBusinessHour(BusinessHour bussinesshour) {
		
	
		 return postForObject(urlBase+urlController+"businesshour/update", bussinesshour, 
	        		new ParameterizedTypeReference<WrapperJSON<BusinessHour>>() {});
	}

	@Override
	public BusinessHour updateBusinessHour(BusinessHour bussinesshour) {
		
		//return restTemplate.postForObject(urlBase+urlController+"calendar/update", bussinesshour, BusinessHour.class);
		 return postForObject(urlBase+urlController+"calendar/update", bussinesshour, 
	        		new ParameterizedTypeReference<WrapperJSON<BusinessHour>>() {});
	}

	@Override
	public void deleteBusinessHour(BusinessHour bussinesshour) {
		
		//restTemplate.postForObject(urlBase+urlController+"businesshour/delete", bussinesshour, Object.class);
		
		postForObject(urlBase+urlController+"businesshour/delete", bussinesshour, 
        		new ParameterizedTypeReference<WrapperJSON<BusinessHour>>() {});
	}
	
	/*
	///////////////////////////////////////////////////////////////////////////////////
	 /////////////////////////////// CALENDAR ///////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////
	*/	
	
	@Override
	public List<Calendar> findAllCalendars(FiltroDTO filtroDTO) {
        
        return postForObject(urlBase+urlController+"calendar/list", filtroDTO, 
        		new ParameterizedTypeReference<WrapperJSON<List<Calendar>>>() {});
	}

	@Override
	public Calendar createCalendar(Calendar calendar) {
		
		 return postForObject(urlBase+urlController+"calendar/save", calendar, 
	        		new ParameterizedTypeReference<WrapperJSON<Calendar>>() {});
	}

	@Override
	public Calendar updatedCalendar(Calendar calendar) {
		
		//return restTemplate.postForObject(urlBase+urlController+"calendar/update", calendar, Calendar.class);
		
		 return postForObject(urlBase+urlController+"calendar/update", calendar, 
	        		new ParameterizedTypeReference<WrapperJSON<Calendar>>() {});
	}

	@Override
	public void deleteCalendar(Calendar calendar) {

		postForObject(urlBase+urlController+"calendar/delete", calendar, 
        		new ParameterizedTypeReference<WrapperJSON<Calendar>>() {});
	}

	/*
	///////////////////////////////////////////////////////////////////////////////////
	 /////////////////////////////// CONDITION ///////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////
	*/

	@Override
	public List<Condition> findAllConditions(FiltroDTO filtroDTO) {
        
        return postForObject(urlBase+urlController+"condition/list", filtroDTO, 
        		new ParameterizedTypeReference<WrapperJSON<List<Condition>>>() {});
	}

	@Override
	public Condition createCondition(Condition condition) {
		
	 return postForObject(urlBase+urlController+"condition/save", condition, 
	        		new ParameterizedTypeReference<WrapperJSON<Condition>>() {});
	}

	@Override
	public Condition updateCondition(Condition condition) {

		 return postForObject(urlBase+urlController+"condition/update", condition, 
	        		new ParameterizedTypeReference<WrapperJSON<Condition>>() {});
	}

	@Override
	public void deleteCondition(Condition condition) {
		
	
		postForObject(urlBase+urlController+"condition/delete", condition, 
        		new ParameterizedTypeReference<WrapperJSON<Condition>>() {});
	}

	/*
	///////////////////////////////////////////////////////////////////////////////////
	 /////////////////////////////// DATASOURCE ///////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////
	*/
	@Override
	public List<Datasource> findAllDatasource(FiltroDTO filtroDTO) {
		
		
        
        return postForObject(urlBase+urlController+"datasource/list", filtroDTO, 
        		new ParameterizedTypeReference<WrapperJSON<List<Datasource>>>() {});
	}

	@Override
	public Datasource createDatasource(Datasource datasource) {

		return postForObject(urlBase+urlController+"datasource/save", datasource, 
        		new ParameterizedTypeReference<WrapperJSON<Datasource>>() {});
	}

	@Override
	public Datasource updateDatasource(Datasource datasource) {
		
	
		return postForObject(urlBase+urlController+"datasource/update", datasource, 
        		new ParameterizedTypeReference<WrapperJSON<Datasource>>() {});
	}

	@Override
	public void deleteDatasource(Datasource datasource) {
	 postForObject(urlBase+urlController+"datasource/delete", datasource, 
	        		new ParameterizedTypeReference<WrapperJSON<Datasource>>() {});
		
	}


	@Override
	public List<ParameterDetail> findDetailList(ParameterDetail listsDetail) {
		
        
        return postForObject(urlBase+urlController+"detaillist/list", listsDetail, 
        		new ParameterizedTypeReference<WrapperJSON<List<ParameterDetail>>>() {});
	}
	
	
	


	/*
	///////////////////////////////////////////////////////////////////////////////////
	 /////////////////////////////// DEPARTMENT /////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////
	*/	
	
	@Override
	public List<Department> findAllDepartments(FiltroDTO filtroDTO) {
		
        
        return postForObject(urlBase+urlController+"department/list", filtroDTO, 
        		new ParameterizedTypeReference<WrapperJSON<List<Department>>>() {});
	}

	@Override
	public Department createDepartment(Department department) {
		
	
		return postForObject(urlBase+urlController+"department/save", department, 
        		new ParameterizedTypeReference<WrapperJSON<Department>>() {});
	}

	@Override
	public Department updateDepartment(Department department) {
		
		return postForObject(urlBase+urlController+"department/update", department, 
        		new ParameterizedTypeReference<WrapperJSON<Department>>() {});
	}

	@Override
	public void deleteDepartment(Department department) {
		
		 postForObject(urlBase+urlController+"department/delete", department, 
	        		new ParameterizedTypeReference<WrapperJSON<Department>>() {});
	}
	
	/*
	///////////////////////////////////////////////////////////////////////////////////
	 /////////////////////////////// PARAMETER DETAIL /////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////
	*/

	public List<ParameterDetail> findAllParameterDetail(FiltroDTO filtroDTO) {
	
        
        return postForObject(urlBase+urlController+"detailparameter/list", filtroDTO, 
        		new ParameterizedTypeReference<WrapperJSON<List<ParameterDetail>>>() {});
	}

	@Override
	public ParameterDetail createParameterDetail(ParameterDetail parameterDetail) {
		
	
		return postForObject(urlBase+urlController+"detailparameter/save", parameterDetail, 
        		new ParameterizedTypeReference<WrapperJSON<ParameterDetail>>() {});
	
	}

	@Override
	public ParameterDetail updateParameterDetail(ParameterDetail parameterDetail) {
		
		
		return postForObject(urlBase+urlController+"detailparameter/update", parameterDetail, 
        		new ParameterizedTypeReference<WrapperJSON<ParameterDetail>>() {});
	
	}

	@Override
	public void deleteParameterDetail(ParameterDetail parameterDetail) {
		
		
		postForObject(urlBase+urlController+"detailparameter/delete", parameterDetail, 
        		new ParameterizedTypeReference<WrapperJSON<ParameterDetail>>() {});
	}
	
	@Override
	public List<ParameterDetail> findStatus() {
	
        

        return postForObject(urlBase+urlController+"detailparameter/status", null,
        		new ParameterizedTypeReference<WrapperJSON<List<ParameterDetail>>>() {});
	}
	
	@Override
	public List<ParameterDetail> findDays() {
		
        
        return postForObject(urlBase+urlController+"detailparameter/days", null,
        		new ParameterizedTypeReference<WrapperJSON<List<ParameterDetail>>>() {});
	}
	
	@Override
	public List<ParameterDetail> findMeses() {
	
        return postForObject(urlBase+urlController+"detailparameter/meses", null,
        		new ParameterizedTypeReference<WrapperJSON<List<ParameterDetail>>>() {});
	}
	
	@Override
	public List<ParameterDetail> findHoras() {
		
        return postForObject(urlBase+urlController+"detailparameter/horas", null,
        		new ParameterizedTypeReference<WrapperJSON<List<ParameterDetail>>>() {});
	}
	
	@Override
	public List<ParameterDetail> findMinutos() {

        
        return postForObject(urlBase+urlController+"detailparameter/minutos", null,
        		new ParameterizedTypeReference<WrapperJSON<List<ParameterDetail>>>() {});
	}
	
	@Override
	public List<ParameterDetail> findTipoFeriados() {

        
        return postForObject(urlBase+urlController+"detailparameter/tipoferiados", null,
        		new ParameterizedTypeReference<WrapperJSON<List<ParameterDetail>>>() {});
	}
	
	@Override
	public List<ParameterDetail> findMetodosLogin() {

        return postForObject(urlBase+urlController+"detailparameter/metodoslogin", null,
        		new ParameterizedTypeReference<WrapperJSON<List<ParameterDetail>>>() {});
        
        
	}

	
	@Override
	public List<ParameterDetail> findParameterDetailByParameter(Parameter parameter) {
	
  
        return postForObject(urlBase+urlController+"detailparameter/listbyparameter", parameter,
        		new ParameterizedTypeReference<WrapperJSON<List<ParameterDetail>>>() {});
        
        
	}


	
	/*
	///////////////////////////////////////////////////////////////////////////////////
	 /////////////////////////////// HOLIDAY ///////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////
	*/

	@Override
	public List<Holiday> findAllHolidays(FiltroDTO filtroDTO) {
	
        
        return postForObject(urlBase+urlController+"holiday/list", filtroDTO,
        		new ParameterizedTypeReference<WrapperJSON<List<Holiday>>>() {});
	}

	@Override
	public Holiday createHoliday(Holiday holiday) {
		
		
        return postForObject(urlBase+urlController+"holiday/save", holiday,
        		new ParameterizedTypeReference<WrapperJSON<Holiday>>() {});
	}

	@Override
	public Holiday updateHoliday(Holiday holiday) {
		
	 return postForObject(urlBase+urlController+"holiday/update", holiday,
	        		new ParameterizedTypeReference<WrapperJSON<Holiday>>() {});
	}

	@Override
	public void deleteHoliday(Holiday holiday) {
		
		postForObject(urlBase+urlController+"holiday/delete", holiday,
        		new ParameterizedTypeReference<WrapperJSON<Holiday>>() {});
	}
	

	/*
	///////////////////////////////////////////////////////////////////////////////////
	 /////////////////////////////// ORGANIZATION ///////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////
	*/
	
	
	
	@Override
	public HashMap<Long,List<Department>> findDepartmenOrganization(Organization organization) { 
        return postForObject(urlBase+urlController+"organization/listDepartmen", organization,
        		new ParameterizedTypeReference<WrapperJSON<HashMap<Long,List<Department>>>>() {});
	}
	@Override
	public List<Organization> findAllOrganizations(FiltroDTO filtroDTO) { 
        
        return postForObject(urlBase+urlController+"organization/list", filtroDTO,
        		new ParameterizedTypeReference<WrapperJSON<List<Organization>>>() {});
	}

	@Override
	public Organization createOrganization(Organization organization) {
	
		 return postForObject(urlBase+urlController+"organization/save", organization,
	        		new ParameterizedTypeReference<WrapperJSON<Organization>>() {});
	}

	@Override
	public Organization updateOrganization(Organization organization) {
		return postForObject(urlBase+urlController+"organization/update", organization,
	        		new ParameterizedTypeReference<WrapperJSON<Organization>>() {});
	}

	@Override
	public void deleteOrganization(Organization organization) {
	postForObject(urlBase+urlController+"organization/delete", organization,
        		new ParameterizedTypeReference<WrapperJSON<Organization>>() {});
	}
	
	
	/*
	///////////////////////////////////////////////////////////////////////////////////
	 /////////////////////////////// PARAMETER ///////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////
	*/
	
	@Override
	public List<Parameter> findAllParameter(FiltroDTO filtroDTO) {
	
        
        return postForObject(urlBase+urlController+"parameter/list", filtroDTO,
        		new ParameterizedTypeReference<WrapperJSON<List<Parameter>>>() {});
	}

	@Override
	public Parameter createParameter(Parameter parameter) {

		return postForObject(urlBase+urlController+"parameter/save", parameter,
        		new ParameterizedTypeReference<WrapperJSON<Parameter>>() {});
	}

	@Override
	public Parameter updateParameter(Parameter parameter) {
		
		return postForObject(urlBase+urlController+"parameter/update", parameter,
        		new ParameterizedTypeReference<WrapperJSON<Parameter>>() {});
	}

	@Override
	public void deleteParameter(Parameter parameter) {
		
		postForObject(urlBase+urlController+"parameter/delete", parameter,
        		new ParameterizedTypeReference<WrapperJSON<Parameter>>() {});
	}

	
	/*
	///////////////////////////////////////////////////////////////////////////////////
	 /////////////////////////////// SECUENCES /////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////
	*/
	
	@Override
	public List<Secuence> findAllSecuences(FiltroDTO filtroDTO) {
		
		//Secuence[] list = restTemplate.postForObject(urlBase+urlController+"secuence/list", filtroDTO, Secuence[].class);
        //return Arrays.asList(list);
        return postForObject(urlBase+urlController+"secuence/list", filtroDTO, 
        		new ParameterizedTypeReference<WrapperJSON<List<Secuence>>>() {});
        
	}

	@Override
	public Secuence createSecuence(Secuence secuence) {
		
		//return restTemplate.postForObject(urlBase+urlController+"secuence/save", secuence, Secuence.class);
		 return postForObject(urlBase+urlController+"secuence/save", secuence, 
	        		new ParameterizedTypeReference<WrapperJSON<Secuence>>() {});
	}

	@Override
	public Secuence updateSecuence(Secuence secuence) {
		
		//return restTemplate.postForObject(urlBase+urlController+"secuence/update", secuence, Secuence.class);
		return postForObject(urlBase+urlController+"secuence/update", secuence, 
        		new ParameterizedTypeReference<WrapperJSON<Secuence>>() {});
	}

	@Override
	public void deleteSecuence(Secuence secuence) {
		
		//restTemplate.postForObject(urlBase+urlController+"secuence/delete", secuence, Object.class);
		postForObject(urlBase+urlController+"secuence/delete", secuence, 
        		new ParameterizedTypeReference<WrapperJSON<Secuence>>() {});
	}
	
	/*
	///////////////////////////////////////////////////////////////////////////////////
	 //////////////////////////// SIST VARIABLE ///////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////
	*/
	
	@Override
	public List<SistVariable> findAllSistVariables(FiltroDTO filtroDTO) {
        
        return postForObject(urlBase+urlController+"sistvariable/list", filtroDTO, 
        		new ParameterizedTypeReference<WrapperJSON<List<SistVariable>>>() {});
	}

	@Override
	public SistVariable createSistVariable(SistVariable sistvariable) {
		
		 return postForObject(urlBase+urlController+"sistvariable/save", sistvariable, 
	        		new ParameterizedTypeReference<WrapperJSON<SistVariable>>() {});
		
	}

	@Override
	public SistVariable updateSistVariable(SistVariable sistvariable) {
		
		return postForObject(urlBase+urlController+"sistvariable/update", sistvariable, 
        		new ParameterizedTypeReference<WrapperJSON<SistVariable>>() {});
	}

	@Override
	public void deleteSistVariable(SistVariable sistvariable) {

		postForObject(urlBase+urlController+"sistvariable/delete", sistvariable, 
        		new ParameterizedTypeReference<WrapperJSON<SistVariable>>() {});
	}
	
	
	
	/*
	///////////////////////////////////////////////////////////////////////////////////
	////////////////////////////////   THEME	///////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////
	*/


	@Override
	public List<Theme> findAllThemes(FiltroDTO filtroDTO) {
	    
        return postForObject(urlBase+urlController+"theme/list", filtroDTO, 
        		new ParameterizedTypeReference<WrapperJSON<List<Theme>>>() {});
	}

	@Override
	public Theme createTheme(Theme theme) {
		  return postForObject(urlBase+urlController+"theme/save", theme, 
	        		new ParameterizedTypeReference<WrapperJSON<Theme>>() {});
	}

	@Override
	public Theme updatedTheme(Theme theme) {
	
		
		return postForObject(urlBase+urlController+"theme/update", theme, 
        		new ParameterizedTypeReference<WrapperJSON<Theme>>() {});
	}

	@Override
	public void deleteTheme(Theme theme) {
		
		postForObject(urlBase+urlController+"theme/delete", theme, 
        		new ParameterizedTypeReference<WrapperJSON<Theme>>() {});
	}

	/*
	///////////////////////////////////////////////////////////////////////////////////
	////////////////////////////////   LOGIN METHOD	///////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////
	*/
	
	@Override
	public List<LoginMethod> findAllLoginMethods(FiltroDTO filtroDTO) {

        
        return postForObject(urlBase+urlController+"loginmethod/list", filtroDTO, 
        		new ParameterizedTypeReference<WrapperJSON<List<LoginMethod>>>() {});
	}

	@Override
	public LoginMethod createLoginMethod(LoginMethod loginMethod) {
		
		  return postForObject(urlBase+urlController+"loginmethod/save", loginMethod, 
	        		new ParameterizedTypeReference<WrapperJSON<LoginMethod>>() {});
	}

	@Override
	public LoginMethod updatedLoginMethod(LoginMethod loginMethod) {
		
		return postForObject(urlBase+urlController+"loginmethod/update", loginMethod, 
        		new ParameterizedTypeReference<WrapperJSON<LoginMethod>>() {});
	}

	@Override
	public void deleteLoginMethod(LoginMethod loginMethod) {

		postForObject(urlBase+urlController+"loginmethod/delete", loginMethod, 
        		new ParameterizedTypeReference<WrapperJSON<LoginMethod>>() {});
	}
}
